package com.decent.ejfadmin.user.controller;

import cn.decent.component.shiro.entity.Admin;
import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.Announcement;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.user.service.UserAnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员公告
 *
 * @author weicx
 * @date 2018/07/26
 */
@RestController
@RequestMapping("/UserAnnouncementController")
@Slf4j
public class UserAnnouncementController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserAnnouncementService userAnnouncementService;

    /**
     * 新增公告
     *
     * @param announcement 公告类
     * @param state        所选会员是否可读：0可读  1不可读
     * @return
     */
    @RequestMapping(value = "addAnnouncement")
    public MessageBean addAnnouncement(Announcement announcement, Integer state) {
        Admin admin = null;
        try {
            admin = adminService.getCurrentAdmin();
            if (admin == null) {
                return MessageBean.fail("请登录后操作");
            }
            if (StringUtils.isBlank(announcement.getContent())) {
                return MessageBean.fail("请选择模板");
            }
            if (StringUtils.isBlank(announcement.getTitle())) {
                return MessageBean.fail("请输入标题");
            }
            if (announcement.getTitle().length() > 40) {
                return MessageBean.fail("标题最长40字");
            }
            if (StringUtils.isBlank(announcement.getAuthor())) {
                return MessageBean.fail("请输入作者");
            }
            if (announcement.getReadable() == null) {
                announcement.setReadable("");
            }
            announcement.setAdmin(admin.getName());
            return userAnnouncementService.addAnnouncement(announcement, state);
        } catch (Exception e) {
            log.warn("新增会员公告异常[{}]", e, e);
            return MessageBean.fail();
        }

    }

    /**
     * 修改公告
     *
     * @param announcement 公告实体类
     * @param state        所选会员是否可读：0可读  1不可读
     * @param operate      操作 0修改 1删除
     * @return
     */
    @RequestMapping(value = "updateAnnouncement")
    public MessageBean updateAnnouncement(Announcement announcement, Integer state, Integer operate) {
        Admin admin = null;
        try {
            admin = adminService.getCurrentAdmin();
            if (operate == null) {
                return MessageBean.fail("请输入操作类型");
            }
            announcement.setAdmin(admin.getName());
            return userAnnouncementService.updateAnnouncement(announcement, state, operate);
        } catch (Exception e) {
            log.warn("修改公告异常[{}]", e, e);
            return MessageBean.fail("修改公告异常");
        }
    }

    /**
     * 查询公告列表
     *
     * @param page      分页参数
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "queryAnnouncement")
    public Page queryAnnouncement(Page page, String beginTime, String endTime) {
        try {
            if (beginTime == null || endTime == null) {
                page.setResultList(new ArrayList());
                return page;
            }
            return userAnnouncementService.queryAnnouncement(page, beginTime, endTime);

        } catch (Exception e) {
            log.warn("查询公告出错{}", e, e);
            page.setResultList(new ArrayList<>());
            page.setTotal(0);
            return page;
        }
    }

    /**
     * 查询公告列表
     *
     * @param id 公告Id
     * @return 公告信息
     */
    @RequestMapping(value = "queryAnnouncementById")
    public Announcement queryAnnouncementById(Integer id) {
        try {
            if (id == null) {
                return null;
            }

            return userAnnouncementService.queryAnnouncementById(id);
        } catch (Exception e) {
            log.warn("查询公告出错{}", e, e);
            return null;
        }
    }

    /**
     * 新增/修改公告模板
     *
     * @param announcement 公告类
     * @param operator     操作 0：新增 1：修改
     * @return
     */
    @RequestMapping(value = "addModel")
    public MessageBean addAnnouncementModel(Announcement announcement, Integer operator) {
        Admin admin = null;
        try {
            admin = adminService.getCurrentAdmin();
            if (admin == null) {
                return MessageBean.fail("请登录后操作");
            }
            if (StringUtils.isBlank(announcement.getContent())) {
                return MessageBean.fail("请输入内容");
            }
            if (StringUtils.isBlank(announcement.getTitle())) {
                return MessageBean.fail("请输入标题");
            }
            return userAnnouncementService.addAnnouncementModel(announcement, operator);
        } catch (Exception e) {
            log.warn("新增会员公告异常[{}]", e);
            return MessageBean.fail();
        }

    }

    /**
     * 查询公告模板
     *
     * @return
     */
    @RequestMapping("getAnnouncementModel")
    public List getAnnouncementModel() {
        try {
            return userAnnouncementService.getAnnouncementModel();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

    }

    /**
     * 查询公告模板内容
     *
     * @return
     */
    @RequestMapping("getAnnouncementModelContent")
    public String getAnnouncementModelContent(Integer id) {
        try {
            return userAnnouncementService.getAnnouncementModelContent(id);
        } catch (Exception e) {
            return "";
        }

    }

    /**
     * 查看/修改公告时转换USERID为USERACCOUNT返回
     *
     * @param ids USERID  会员ID
     * @return
     */
    @RequestMapping(value = "getReadableAccount")
    public String getReadableAccount(String ids) {
        if (StringUtils.isBlank(ids)) {
            return null;
        }
        return userAnnouncementService.getReadableAccount(ids);
    }

}
